package com.demo.consumer.listeners;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * <h3>mq-demo</h3>
 * <p></p>
 *
 * @author : zhengyue
 * @date : 2023/10/27 11:46
 **/
@Slf4j
@Component
public class MqListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueue(String msg) {
//        System.out.println("消费者收到了simple.queue的消息：【" + msg + "】");
//        throw new RuntimeException("手动异常");
//    }

    @RabbitListener(queues = "work.queue")
    public void listenSimpleQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1 收到了work.queue的消息：【" + msg + "】");
        Thread.sleep(20);

    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) {
        System.out.println("消费者1 收到了fanout.queue的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) {
        System.err.println("消费者2 收到了fanout.queue的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者1 direct.queue的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String msg) {
        System.err.println("消费者2 direct.queue的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String msg) {
        System.err.println("消费者1 topic.queue的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String msg) {
        System.err.println("消费者2 topic.queue的消息：【" + msg + "】");
    }

    /**
     *     SpringAMQP 还提供了基于@RabbitListener注解来声明队列和交换机的方式
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "anno.direct.queue1", durable = "true"),
            exchange = @Exchange(name = "anno.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
    ))
    public void annoDirectQueue(String msg) {
        System.out.println("消费者1 anno.queue的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "anno.direct.queue2", durable = "true"),
            exchange = @Exchange(name = "anno.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void annoDirectQueue2(String msg) {
        System.err.println("消费者2 anno.queue的消息：【" + msg + "】");
    }

//    @RabbitListener(queuesToDeclare =  @Queue(name = "lazy.queue", durable = "true",
//            arguments = @Argument(name = "x-queue-mode", value = "lazy")
//    ))
//    public void listenLazyQueue(String msg) {
//        System.out.println("消费者 lazy.queue的消息：【" + msg + "】");
//    }

    @RabbitListener(queues = "dlx.queue")
    public void listenDlxQueue(String msg) {
        log.info("dlx.queue的消息：【" + msg + "】");
    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "delay.queue", durable = "true"),
//            exchange = @Exchange(name = "delay.direct", delayed="true"),
//            key = "delay"
//    ))
//    public void listenDelayMessage(String msg) {
//        log.info("接收到 delay.queue的延迟消息消息：【" + msg + "】");
//    }
}
